package cn.itcast.bos.web.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	protected T model;
	@Override
	public T getModel() {
		return model;
	}
	//泛型在运行期是没用的的，所以通过构造器让model实例化
	public BaseAction() {
		//通过其子类和其泛型获得带有泛型的自己ModelDriven<Model>
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		//其本身又是抽象的，不能实例化，转化为可参数花的类型
		ParameterizedType parameterizedType=(ParameterizedType)genericSuperclass;
		//但是我们要的其泛型
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		//获得泛型的实例对象
		try {
			model=modelClass.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("模型构造失败");
		}
	}
	
	//分页查询的代码重构
	
	
	//接收分页的查询参数
	protected int page;
	protected int rows;
	
	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	

	protected void pushPageDataToValueStack(Page<T> page) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());
		
		ActionContext.getContext().getValueStack().push(map);
	}
	
	//作废功能的抽取
	protected  String ids;

	public void setIds(String ids) {
		this.ids = ids;
	}

	protected String tag;
	
	public void setTag(String tag) {
		this.tag = tag;
	}
}
